using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Rogue
{
    // A window for showing text (instead of terrain-ish graphics cells)
    // Has specialized offsets for packing characters together.

    public class TextWindow : CellWindow
    {
        #region Public Member Variables
        public override int WindowWidth
            { get { return m_aCellMap.GetLength(0) * (m_pCellScreenSize.X + m_pCharacterOffset.X); } }

        public override int WindowHeight
            { get { return m_aCellMap.GetLength(1) * (m_pCellScreenSize.Y + m_pCharacterOffset.Y); } }
        #endregion

        #region Protected Member Variables
        protected Point m_pCharacterOffset;
        #endregion

        public TextWindow(
            Texture2D a_Tex,
            GraphicsDevice a_Dev,
            Point a_Pos,
            Point a_pCellMapSize,
            Point a_pCellScreenSize,
            Point a_pCharacterOffset
            )
            : base(a_Tex, a_Dev, a_Pos, a_pCellMapSize, a_pCellScreenSize)
        {
            m_pCharacterOffset = a_pCharacterOffset;
        }

        #region Rendering Overrides
        public override Rectangle GetRenderSource(int a_X, int a_Y)
        {
            return m_aCellDefs[(int)m_aCellMap[a_X, a_Y].ForegroundTile];
        }

        public override Rectangle GetRenderDest(int a_X, int a_Y)
        {
            return new Rectangle(
                (a_X * (m_pCellScreenSize.X + m_pCharacterOffset.X)) + Position.X, 
                (a_Y * (m_pCellScreenSize.Y + m_pCharacterOffset.Y)) + Position.Y, 
                m_pCellScreenSize.X, 
                m_pCellScreenSize.Y
                );
        }

        public override void PutFloatingString(string a_sString, Point a_pDest, Color a_Color)
        {
            PutFloatingString(a_sString, a_pDest, a_Color, null, m_pCellScreenSize, m_pCharacterOffset);
        }

        public override void RenderFloatingStrings()
        {
            for (int i = 0; i < m_nFloatingStringCount; i++)
            {
                for (int j = 0; j < m_FloatingStrings[i].s.Length; j++)
                {
                    int nOffsetX = j * (m_pCellScreenSize.X + m_pCharacterOffset.X);
                    char c = m_FloatingStrings[i].s[j];
                    if (c != ' ')
                    {
                        m_SpriteBatch.Draw(
                            m_Texture,
                            new Vector2(m_FloatingStrings[i].Position.X + nOffsetX, m_FloatingStrings[i].Position.Y),
                            m_aCellDefs[(int)c],
                            new Color(m_FloatingStrings[i].Color)
                            );

                    }
                }
            }
            m_nFloatingStringCount = 0;
        }

        public void PutFloatingText(string[] a_aStrings, Point a_pTopLeft, Color a_Color)
        {
            Point pCurrentOffset = a_pTopLeft;
            for (int i = 0; i < a_aStrings.Length; i++)
            {
                pCurrentOffset.Y = a_pTopLeft.Y + (i * m_pCellScreenSize.Y);
                PutFloatingString(a_aStrings[i], pCurrentOffset, a_Color);
            }
        }

        public void PutFloatingText(string[] a_aStrings, Point a_pTopLeft, Color[] a_aColors)
        {
            if (a_aStrings.Length != a_aColors.Length)
                return;

            Point pCurrentOffset = a_pTopLeft;
            for (int i = 0; i < a_aStrings.Length; i++)
            {
                pCurrentOffset.Y = a_pTopLeft.Y + (i * m_pCellScreenSize.Y);
                PutFloatingString(a_aStrings[i], pCurrentOffset, a_aColors[i]);
            }
        }
        #endregion

        #region Member Overrides
        public override Rectangle GetLocalCellBounds(int a_X, int a_Y)
        {  
            Rectangle rBase = base.GetLocalCellBounds(a_X, a_Y);
            rBase.Offset(m_pCharacterOffset.X * a_X, m_pCharacterOffset.Y * a_Y);
            return rBase;
        }
        #endregion


    }
}
